package com.dmz.wxcp.utils;

import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * @description:
 * @author: pilaf
 * @create: 2019-04-23 15:10
 */
public class HttpClientUtils {

    /**
     * 响应处理器
     */
    private static ResponseHandler<JSONObject> responseHandler = response -> {
        int statusCode = response.getStatusLine().getStatusCode();
        //正常响应
        if (statusCode >= 200 && statusCode < 300) {
            HttpEntity entity = response.getEntity();
            if(entity!=null){
                return getJsonObject(entity);
            }else {
                return null;
            }
        }else {
            throw new ClientProtocolException("Unexpected response status:" + statusCode);
        }
    };

    /**
     * 发送get请求
     *
     * @param url 请求url
     * @return 响应JSON
     */
    public static JSONObject sendGet(String url) {
        //创建连接
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            //创建HttpGet实例
            HttpGet httpGet = new HttpGet(url);
            JSONObject responseBody = httpClient.execute(httpGet, responseHandler);
            return responseBody;
        } catch (IOException e) {
            return null;
        }

    }

    /**
     * 发送http post请求
     * @param url 请求url
     * @param obj 请求体对象
     * @return
     */
    public static JSONObject sendPost(String url, Object obj) {
        JSONObject jsonObject = JSONObject.fromObject(obj);
        String jsonStr = jsonObject.toString();
        return sendPost(url, jsonStr);
    }

    /**
     * 发送http post请求
     * @param url 请求url
     * @param jsonContext 请求体json
     * @return
     */
    public static JSONObject sendPost(String url, String jsonContext) {
        HttpPost httpPost = new HttpPost(url);

        StringEntity stringEntity = new StringEntity(jsonContext, ContentType.create("text/plain", "UTF-8"));

        //设置需要传递的数据
        httpPost.setEntity(stringEntity);

        //创建一个HttpClient连接
        try( CloseableHttpClient httpClient = HttpClients.createDefault()){
            JSONObject responseBody = httpClient.execute(httpPost, responseHandler);
            return responseBody;
        }catch (IOException e){
            return null;
        }

    }

    /**
     * 将HttpEntity转换成JSONObject对象
     * @param entity
     * @return
     * @throws IOException
     */
    private static JSONObject getJsonObject(HttpEntity entity) throws IOException {
        String result = EntityUtils.toString(entity);
        //根据字符串生成JSONObject对象
        JSONObject resultObj = JSONObject.fromObject(result);
        return resultObj;
    }

}
